﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SWDataDB.DAL;
using SWDataDB.Models;
using System.Data.SqlClient;
namespace SWDataDB.DAL
{
    /// <summary>
    /// 由于数据结构和数据库表不对应，暂时没有完成
    /// 2013-05-19：zhhw
    ///  其他实物
    /// </summary>
    public class UTILInfo
    {
        public UTILInfo() { }

        /// <summary>
        /// 获取所有信息
        /// </summary>
        /// <returns>返回dataSet</returns>
        public DataSet GetAllInfoBySet()
        {
            string strSql = "SELECT * FROM dbo.UTIL_TBL";
            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 获取所有信息
        /// </summary>
        /// <returns>返回datatable</returns>
        public DataTable GetAllInfoByTable()
        {
            string strSql = "SELECT * FROM dbo.UTIL_TBL";
            return SQLHelper.ExecuteReaderDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 得到记录for 分页显示，升序排列
        /// </summary>
        /// <param name="page"></param>
        /// <param name="num"></param>
        /// <param name="strExp"></param>
        /// <returns>返回dataset</returns>
        public DataSet GetInfoForPageByASC(string strExp)
        {
            string strSql = "SELECT * FROM dbo.UTIL_TBL WHERE ";
            strSql += strExp;
            strSql += "ORDER BY ID ASC";
            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 得到著录表记录for 分页显示，降序排列
        /// </summary>
        /// <param name="page"></param>
        /// <param name="num"></param>
        /// <param name="strExp"></param>
        /// <returns>返回dataset</returns>
        public DataSet GetInfoForPageByDESC(string strExp)
        {
            string strSql = "SELECT * FROM dbo.UTIL_TBL WHERE ";
            strSql += strExp;
            strSql += "ORDER BY ID DESC";
            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 分页查询 后台控制页面大小
        /// </summary>
        /// <param name="page"></param>
        /// <param name="num"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public IList<UTIL_TBL> GetInfoByPage(long page, long num, string strWhere)
        {
            IList<UTIL_TBL> utilLst = new List<UTIL_TBL>();
            string sql = SQLHelper.GetPagingSQL("dbo.UTIL_TBL", "ID", "*", strWhere,
                "ID DESC", num, page);

            DataTable dt = SQLHelper.ExecuteReaderDataSet(SQLHelper.SQLConnString, CommandType.Text, sql, null);
            try
            {
                foreach (DataRow dr in dt.Rows) 
                {
                    UTIL_TBL info = new UTIL_TBL();
                    info.O_ID = int.Parse(dr["ID"].ToString());
                    info.O_ZL_FILE_ID = dr["ZL_FILE_ID"].ToString();
                    info.O_COL_DATE = dr["COL_DATE"].ToString();
                    info.O_COL_NAME = dr["COL_NAME"].ToString();
                    info.O_SW_NUM = int.Parse(dr["SW_NUM"].ToString());
                    info.O_SW_TYPE = dr["SW_TYPE"].ToString();

                    utilLst.Add(info);
                }

                return utilLst;
            }
            catch (ArgumentNullException e)
            {
                throw e;
            }
            catch (FormatException e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 通过ID查询记录
        /// </summary>
        /// <param name="file_id"></param>
        /// <returns></returns>
        public UTIL_TBL SelectUTILInfoById(string file_id)
        {
            UTIL_TBL info = new UTIL_TBL();
            string strSql = "SELECT * FROM dbo.UTIL_TBL WHERE FILE_ID=";
            strSql += file_id;

            DataTable dt = SQLHelper.ExecuteReaderDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
            try
            {
                foreach (DataRow dr in dt.Rows)
                {

                }

                return info;
            }
            catch (ArgumentNullException e)
            {
                throw e;
            }
            catch (FormatException e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 根据外键查询
        /// </summary>
        /// <param name="zl_file_id"></param>
        /// <returns></returns>
        public DataSet SelectInfoByFileID(string zl_file_id)
        {
            string strSql = "SELECT * FROM dbo.UTIL_TBL WHERE ZL_FILE_ID ='";
            strSql += zl_file_id + "'";

            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }



        public DataSet SelectInfoByID(int id)
        {
            string strSql = "SELECT * FROM dbo.UTIL_TBL WHERE ID ="+id;
            
            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        public int AppendUTILInfo(UTIL_TBL info)
        {
            string strSql = @"INSERT INTO dbo.UTIL_TBL
               ([ZL_FILE_ID]
               ,[SW_TYPE]
               ,[SW_NUM]
               ,[COL_NAME]
               ,[COL_DATE])
         VALUES ('{0}', '{1}', {2}, '{3}', '{4}')";

            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat(strSql, info.O_ZL_FILE_ID, info.O_SW_TYPE, info.O_SW_NUM, 
                info.O_COL_NAME, info.O_COL_DATE);
            // 是否需要修改ZL_TBL中的信息

            return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, sbSql.ToString(), null);
        }

        /// <summary>
        /// 通过file_ID 删除
        /// </summary>
        /// <param name="file_id"></param>
        /// <returns></returns>
        public int DeleteUtilInfoById(int id)
        {
            string strSql = "DELETE FROM dbo.UTIL_TBL WHERE ID = ";
            strSql += id.ToString();

            return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        public int UpdateUtilInfo(UTIL_TBL info)
        {
            /*FILE_ID,ZK_CODE,KT_LCODE,ZK_LONGITUDE,ZK_LATITUDE,ZK_X,ZK_Y,ZK_H,
            ZK_DEPTH,ZK_CORELEN,GC_CORELEN,ZKYX_COUNT,GCYX_COUNT,START_DATE,END_DATE;*/

            string strSql = "UPDATE dbo.UTIL_TBL ";
            strSql += "SET SW_TYPE='" + info.O_SW_TYPE + "',";
            strSql += " SW_NUM='" + info.O_SW_NUM.ToString() + "',";
            strSql += "  COL_NAME='" + info.O_COL_NAME + "',";
            strSql += "  COL_DATE='" + info.O_COL_DATE + "',";
            strSql += " ZL_FILE_ID='" + info.O_ZL_FILE_ID + "'";
            strSql += " WHERE ID=" + info.O_ID;

            return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        #region =影像数据操作=
        public int AppendImage(int id, byte[] bImg, long len)
        {
            string SQL = "UPDATE  dbo.UTIL_TBL SET IMAGE = @IMAGE WHERE ID=" + id.ToString();

            SqlParameter[] commandParameters = new SqlParameter[1];
            commandParameters[0] = new SqlParameter("@IMAGE", SqlDbType.Image);
            commandParameters[0].Value = bImg;

            try
            {
                SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, SQL, commandParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }


            return 1;
        }

        public byte[] GetImage(int id)
        {
            byte[] photo = null;
            string sql = "SELECT IMAGE FROM dbo.UTIL_TBL WHERE ID =" + id.ToString();
            SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.SQLConnString, CommandType.Text, sql, null);//采用SqlDataReader的方法来读取数据

            if (reader.Read())
                photo = reader[0] as byte[];//将第0列的数据写入byte数组 

            reader.Close();                 //关闭reader
            return photo;
        }

        #endregion
    }

//         /// <summary>
//         /// 添加记录
//         /// </summary>
//         /// <param name="yx_info"></param>
//         /// <returns></returns>
//         public int AddYXInfo(YX_TBL yx_info)
//         {
//             // 先处理普通数据
//             string strSql = "INSERT INTO dbo.YX_TBL(FILE_ID,ZK_CODE,KT_LCODE,ZK_LONGITUDE,ZK_LATITUDE,";
//             strSql += "ZK_X,ZK_Y,ZK_H,ZK_DEPTH,ZK_CORELEN,GC_CORELEN,ZKYX_COUNT,GCYX_COUNT,";
//             strSql += "START_DATE,END_DATE) VALUES(";
//             strSql += "'" + yx_info.O_FILE_ID + "',";
//             strSql += "'" + yx_info.O_ZK_CODE + "',";
//             strSql += "'" + yx_info.O_KT_LCODE + "',";
//             strSql += yx_info.O_ZK_LONGITUDE.ToString() + ",";
//             strSql += yx_info.O_ZK_LATITUDE.ToString() + ",";
//             strSql += yx_info.O_ZK_X.ToString() + ",";
//             strSql += yx_info.O_ZK_Y.ToString() + ",";
//             strSql += yx_info.O_ZK_H.ToString() + ",";
//             strSql += yx_info.O_ZK_DEPTH.ToString() + ",";
//             strSql += yx_info.O_ZK_CORELEN.ToString() + ",";
//             strSql += yx_info.O_GC_CORELEN.ToString() + ",";
//             strSql += yx_info.O_ZKYX_COUNT.ToString() + ",";
//             strSql += yx_info.O_GCYX_COUNT.ToString() + ",";
//             strSql += "'" + yx_info.O_START_DATE.ToString() + "',";
//             strSql += "'" + yx_info.O_END_DATE.ToString() + "')";
//           
//             return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
//         }
// 
//         /// <summary>
//         /// 通过file_ID 删除岩心记录
//         /// </summary>
//         /// <param name="file_id"></param>
//         /// <returns></returns>
//         public int DeleteYXInfoById(string file_id)
//         {
//             string strSql = "DELETE FROM dbo.YX_TBL WHERE FILE_ID ='";
//             strSql += file_id + "'";
// 
//             return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
//         }
// 
//         /// <summary>
//         /// 更新岩心记录
//         /// </summary>
//         /// <param name="yx_info"></param>
//         /// <returns></returns>
//         public int UpdateYXInfo(YX_TBL yx_info)
//         {
//             /*FILE_ID,ZK_CODE,KT_LCODE,ZK_LONGITUDE,ZK_LATITUDE,ZK_X,ZK_Y,ZK_H,
//             ZK_DEPTH,ZK_CORELEN,GC_CORELEN,ZKYX_COUNT,GCYX_COUNT,START_DATE,END_DATE;*/
// 
//             string strSql = "UPDATE dbo.YX_TBL ";
//             strSql += "SET ZK_CODE='" + yx_info.O_ZK_CODE + "',";
//             strSql += "SET KT_LCODE='" + yx_info.O_KT_LCODE + "',";
//             strSql += "SET ZK_LONGITUDE=" + yx_info.O_ZK_LONGITUDE + ",";
//             strSql += "SET ZK_LATITUDE=" + yx_info.O_ZK_LATITUDE + ",";
//             strSql += "SET ZK_X=" + yx_info.O_ZK_X.ToString() + ",";
//             strSql += "SET ZK_Y=" + yx_info.O_ZK_Y.ToString() + ",";
//             strSql += "SET ZK_H=" + yx_info.O_ZK_H.ToString() + ",";
//             strSql += "SET ZK_DEPTH=" + yx_info.O_ZK_DEPTH.ToString() + ",";
//             strSql += "SET ZK_CORELEN=" + yx_info.O_ZK_CORELEN.ToString() + ",";
//             strSql += "SET GC_CORELEN=" + yx_info.O_GC_CORELEN.ToString() + ",";
//             strSql += "SET ZKYX_COUNT=" + yx_info.O_ZKYX_COUNT.ToString() + ",";
//             strSql += "SET GCYX_COUNT=" + yx_info.O_GCYX_COUNT.ToString() + ",";
//             strSql += "SET START_DATE='" + yx_info.O_START_DATE.ToString() + "',";
//             strSql += "SET END_DATE='" + yx_info.O_END_DATE.ToString() + "',";
//          
//             strSql += "WHERE FILE_ID='" + yx_info.O_FILE_ID + "'";
// 
//             return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
//         }
//     }
}
